package com.ybbase.framework.config.redis;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import java.time.Duration;
/**
 * Redis配置类
 * @author cg
 * @date 2019-11-19 11:34
 * @since 4.0
 */
//@Configuration
//@EnableCaching
public class RedisConfig {
   // @Value("${spring.redis.host}")
   // private String host;
   // @Value("${spring.redis.port}")
   // private int port;
   // @Value("${spring.redis.timeout}")
   // private String timeout;
   // @Bean
   // public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
   //     /**
   //      * 设置缓存有效期一小时
   //      * */
   //     RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
   //             .entryTtl(Duration.ofHours(Long.parseLong(timeout.replace("ms", ""))));
   //     return RedisCacheManager
   //             .builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
   //             .cacheDefaults(redisCacheConfiguration).build();
   // }
   // @Bean
   // public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory){
   //     StringRedisTemplate template = new StringRedisTemplate(factory);
   //     /**
   //      * 设置序列化工具
   //      * */
   //     setSerializer(template);
   //     template.afterPropertiesSet();
   //     return template;
   // }
   // /**
   //  * 序列化定义
   //  * */
   // private void setSerializer(StringRedisTemplate template){
   //     @SuppressWarnings({ "rawtypes", "unchecked" })
   //     Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
   //     ObjectMapper om = new ObjectMapper();
   //     om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
   //     om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
   //     jackson2JsonRedisSerializer.setObjectMapper(om);
   //     template.setValueSerializer(jackson2JsonRedisSerializer);
   // }

}
